package vocation;

import com.jfinal.core.Controller;
import common.model.User;
import login.LoginService;

import java.util.Date;

public class VocationController extends Controller {


    private static final VocationService srv = VocationService.me;

    public void index() {
        render("index.html");
    }

    public void getVocation() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        String status = getPara("status");
        int me = getParaToInt("me");
        if (me == 1) {
            status = "我申请的";
        }
        renderJson(srv.getVocationString(user, status));
    }

    public void newVocation() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        String reason = getPara("reason");
        Date startDay = getDate("startDay");
        Date endDay = getDate("endDay");
        renderJson(srv.newVocation(user, reason, startDay, endDay));
    }

    public void agreeVocation() {
        int id = getParaToInt("id");
        renderJson(srv.agreeVocation(id));
    }

    public void refuseVocation() {
        int id = getParaToInt("id");
        renderJson(srv.refuseVocation(id));
    }

    public void deleteVocation() {
        User user = LoginService.me.validateToken(getCookie(LoginService.USER));
        int id = getParaToInt("id");
        renderJson(srv.deleteVocation(id, user));
    }
}
